package org.dallasrowing.model;

import java.util.Date;
import java.util.List;

public class LogBookEntry {

	private Date entryOpenDate;
	private Boat boat;
	private List<Rower> rowers;

	private Date entryCloseDate;
	private String comment;
	private Double distanceRowed;
	
	public LogBookEntry( Date date, Boat boat, List<Rower> participants )
	{
		this.entryOpenDate = date;
		this.boat = boat;
		this.rowers = participants;
	}

	public LogBookEntry()
	{
		
	}
	
	public Date getEntryOpenDate() {
		return entryOpenDate;
	}


	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = super.hashCode();
		result = PRIME * result + ((boat == null) ? 0 : boat.hashCode());
		result = PRIME * result + ((comment == null) ? 0 : comment.hashCode());
		result = PRIME * result + ((distanceRowed == null) ? 0 : distanceRowed.hashCode());
		result = PRIME * result + ((entryCloseDate == null) ? 0 : entryCloseDate.hashCode());
		result = PRIME * result + ((entryOpenDate == null) ? 0 : entryOpenDate.hashCode());
		result = PRIME * result + ((rowers == null) ? 0 : rowers.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		final LogBookEntry other = (LogBookEntry) obj;
		if (boat == null) {
			if (other.boat != null)
				return false;
		} else if (!boat.equals(other.boat))
			return false;
		if (comment == null) {
			if (other.comment != null)
				return false;
		} else if (!comment.equals(other.comment))
			return false;
		if (distanceRowed == null) {
			if (other.distanceRowed != null)
				return false;
		} else if (!distanceRowed.equals(other.distanceRowed))
			return false;
		if (entryCloseDate == null) {
			if (other.entryCloseDate != null)
				return false;
		} else if (other.entryCloseDate == null || !entryCloseDate.toString().equals(other.entryCloseDate.toString()))
			return false;
		if (entryOpenDate == null) {
			if (other.entryOpenDate != null)
				return false;
		} else if (!entryOpenDate.toString().equals(other.entryOpenDate.toString()))
			return false;
		if (rowers == null) {
			if (other.rowers != null)
				return false;
		} else if (!rowers.equals(other.rowers))
			return false;
		return true;
	}

	public String toString()
	{
		return 
			"Start Date: " + getEntryOpenDate() + "\n" +
			"End Date: " + getEntryCloseDate() + "\n" +
			"Rowers: " + getRowers() + "\n" + 
			"Distance rowed: " + getDistanceRowed() + "\n" + 
			"Comment: " + getComment() + "\n";
	}

	public Boat getBoat() {
		return boat;
	}

	public List<Rower> getRowers() {
		return rowers;
	}

	public boolean isValid() {
		return
			getBoat() != null &&
			getEntryOpenDate() != null &&
			getRowers() != null &&
			boatProperlyEquipped();
	}

	private boolean boatProperlyEquipped() {
		return getRowers().size() == getBoat().getSeats() + 
		                             ( getBoat().isWithCox() ? 1 : 0 );
	}

	@SuppressWarnings( "unused" )
	private void setBoat(Boat boat) {
		this.boat = boat;
	}

	@SuppressWarnings( "unused" )
	private void setEntryOpenDate(Date entryStartDate) {
		this.entryOpenDate = entryStartDate;
	}

	@SuppressWarnings( "unused" )
	private void setRowers(List<Rower> rowers) {
		this.rowers = rowers;
	}

	public void setEntryCloseDate(Date entryCloseDate) {
		this.entryCloseDate = entryCloseDate; 
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Double getDistanceRowed() {
		return distanceRowed;
	}

	public void setDistanceRowed(Double d) {
		this.distanceRowed = d;
	}

	public Date getEntryCloseDate() {
		return entryCloseDate;
	}

}
